package com.yeung.swordoffer;

import java.util.Stack;

/**
 * 输入一个链表，输出该链表中倒数第k个结点。
 * <p>
 * 应用递归的思想
 */
public class 反转链表 {
    public static void main(String[] args) {

    }

    public static ListNode FindKthToTail(ListNode head) {
        if (head == null) return null;
        Stack<ListNode> stack = new Stack<>();
        while (head != null) {
            stack.push(head);
            head = head.next;
        }
        ListNode currNode = null;
        ListNode root = null;
        while (!stack.isEmpty()) {
            ListNode node = stack.pop();
            node.next = null;
            if (currNode == null) {
                root = node;
                currNode = root;
            } else {
                currNode.next = node;
                currNode = node;
            }
        }
        return root;

    }

    static class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }
}
